package com.astrofizzbizz.stoneedgeqb;

import java.util.ArrayList;

import com.astrofizzbizz.simpleXml.SimpleXmlDoc;
import com.astrofizzbizz.simpleXml.SimpleXmlException;
import com.astrofizzbizz.simpleXml.SimpleXmlReader;
import com.astrofizzbizz.simpleXml.SimpleXmlWriter;

@SuppressWarnings("serial")
public class AstroTargetList extends ArrayList<AstroTarget>
{
	private boolean debug = false;
	private boolean verbose = false;

	public boolean isVerbose() {return verbose;}
	public boolean isDebug() {return debug;}
	public void setDebug(boolean debug) {this.debug = debug;}
	public void setVerbose(boolean verbose) {this.verbose = verbose;}

	public AstroTargetList(boolean debug, boolean verbose)
	{
		super();
		this.debug = debug;
		this.verbose = verbose;
	}
	public AstroTargetList(String filePath, boolean debug, boolean verbose) throws StoneEdgeException
	{
		this(debug, verbose);
		try 
		{
			SimpleXmlDoc xdoc = new SimpleXmlDoc(filePath);
			SimpleXmlReader catalog = new SimpleXmlReader(xdoc);
			SimpleXmlReader cataloglist = catalog.tag(0);
			int numTargets = cataloglist.numChildTags();
			for (int ii = 0; ii < numTargets; ++ii) add(new AstroTarget(cataloglist.tag(ii), debug, verbose));
		} 
		catch (SimpleXmlException e) 
		{
			throw new StoneEdgeException(e);
		}
	}
	public int numTargets()
	{
		return size();
	}
	public AstroTargetList getVisibleTargets() throws StoneEdgeException
	{
		AstroTargetList visibleTargetList = new AstroTargetList(debug, verbose);
		for (int ii = 0; ii < numTargets(); ++ii) 
		{
			if (get(ii).isVisible()) 
			{
				visibleTargetList.add(get(ii));
			}
		}
		return visibleTargetList;
	}
	public void writeList(String filePath) throws StoneEdgeException
	{
		try 
		{
			SimpleXmlWriter xw = new SimpleXmlWriter("stoneEdgeQb");
			xw.openXmlTag("catalog");
			for (int ii = 0; ii < numTargets(); ++ii) get(ii).writeTag(xw);
			xw.closeXmlTag("catalog");
			xw.saveXmlDocument(filePath);
		} 
		catch (SimpleXmlException e) 
		{
			throw new StoneEdgeException(e);
		}
	}
	public static void main(String[] args) throws StoneEdgeException 
	{
		AstroTargetList astroTargetList = new AstroTargetList("stoneEdgeQb.xml", true, true);
		AstroTargetList visibleTargetList = astroTargetList.getVisibleTargets();
		for (int ii = 0; ii < visibleTargetList.numTargets(); ++ii) System.out.println(visibleTargetList.get(ii).getName() + " is visibile");
		visibleTargetList.writeList("visibleTarget.xml");
	}

}
